﻿using System;
using System.Linq;
using System.Threading;
using BoardPACDSWeb.AnonymousUserServiceReference;

namespace BoardPACDSWeb.Models.General
{
    public static class Logs
    {
        //private delegate void AsyncDelegate(string info);
        //private static AsyncDelegate _asyncDel;

        //static Logs()
        //{
            //_asyncDel = new AsyncDelegate(WriteLogToDb);
        //}

        /// Author: Suren Manawatta.
        /// //Date: 2012-06-14.
        /// <summary>
        /// This method used to write a string log information asynchronously to the database. (Logs table)
        /// </summary>
        /// <param name="logData">The log information needed to be saved on the database</param>
        /*public static void WriteLogToDbAsync(string logData)
        {
            try
            {
                _asyncDel.BeginInvoke(logInfo, null, null);
            }
            catch (Exception)
            {
                throw;
            }
        }*/


        /// Author: Suren Manawatta.
        /// //Date: 2012-06-14.
        /// <summary>
        /// This method used to write a string log information to the database. (Logs table)
        /// </summary>
        /// <param name="logInfo">The log information needed to be saved on the database</param>
        public static void WriteLogToDb(string logInfo = "")
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(AddLog), logInfo);
        }

        private static void AddLog(object text)
        {
            string logInfo = text.ToString();
            try
            {
                using (AnonymousUserServiceClient client = new AnonymousUserServiceClient())
                {
                    int logId = client.AddLog(logInfo);
                }

                #region Commented

                /*
                using (BoardAppProductEntities bape = new BoardAppProductEntities())
                {
                    AuditLog log = new AuditLog();
                    string t = logInfo;
                    string[] array = t.Split(',');

                    if (array.Count() > 6)
                    {
                        log.EventNumber = Convert.ToInt16(array[0].Trim());
                        log.Event = array[1].Trim();
                        log.EventStatus = array[2].Trim();
                        log.ControllerName = array[3].Trim();
                        log.ActionName = array[4].Trim();
                        log.UserName = array[5].Trim();
                        
                        log.Date = DateTime.Now;

                        string tempPara = "";
                        for (int i = 6; i < array.Count(); i++)
                        {
                            tempPara += array[i].Trim() + ", ";
                        }
                        char[] lop = {' ', ','};
                        tempPara = tempPara.TrimEnd(lop);
                        log.Parameters = tempPara;

                        bape.AddToAuditLogs(log);
                        bape.SaveChanges();
                    }
                }*/

                #endregion
            }
            catch (Exception)
            {
            }
        }
    }
}
